<?php
/***************************************************************************
 *   Copyright (C) 2008 by Julian Cromarty and Stuart (BPX-Dev)            *
 *   julian.cromarty@gmail.com or stu@broken-paradox.com                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/**
* 
* Class to view posts and comments
*
*@author Julian Cromarty
*/
class viewPosts	{
/**
*
* Variable that stores the database
*
*/
	private $dbcon;
	
/**
*
* Constructor. Stores the passed database connection to a class variable
*
*@param $dbcon A mysqli database connection object
*/
	function __construct($con)	{
		$this->dbcon = $con;
		$this->numPosts = null;
	}
	
/**
*
* Gets the total number of posts in the database
*
*@return The number of posts
*/
	function getNumPosts()	{
		$result = $this->dbcon->query("SELECT COUNT(post_id) FROM posts");
		$fieldinfo = $result->fetch_field();
		$row = $result->fetch_assoc();
		return $row[$fieldinfo->name];
	}

/**
*
* Gets some posts from the database
*
*@param $startNum The first post_id of the set to be retrieved
*@param $pPP The number of posts-per-page to display
*@return An array of arrays of post data
*/
	function getPosts($startId, $pPP)	{
		$stmt = $this->dbcon->prepare('SELECT * FROM posts WHERE post_id >= ? LIMIT ?');
		$stmt->bind_param('ii', $startId, $pPP);
		$stmt->execute();
		$results[] = array();
		$stmt->bind_result($pId, $pTitle, $pPost, $ptime, $pAuthorId, $pTags);
		$i = 0;
		while ($stmt->fetch())	{
			$results[$i]['pId'] = $pId;
			$results[$i]['pTitle'] = $pTitle;
			$results[$i]['pContent'] = $pPost;
			$results[$i]['pTime'] = $ptime;
			$results[$i]['pAuthor'] = $pAuthorId;
			$results[$i]['pTags'] = $pTags;
			$i++;
		}
		foreach ($results as &$j)	{
			$aID = $j['pAuthor'];
			$j['pAuthor'] = $this->getAuthorNameFromID((int)$aID);
			$tags = $j['pTags'];
			$j['pTags'] = $this->splitTags($tags);
			$j['numComments'] = $this->getNumComments($j['pId']);
		}
		return $results;
	}
	
/**
 *
 * Gets a single post from the database
 *
 *@param $startNum The post_id of the post to be retrieved
 *@return An array with the details of the post
 */
	function getPost($postId)	{
		$stmt = $this->dbcon->prepare('SELECT * FROM posts WHERE post_id = ? LIMIT 1');
		$stmt->bind_param('i', $postId);
		$stmt->execute();
		$results[] = array();
		$stmt->bind_result($pId, $pTitle, $pPost, $ptime, $pAuthorId, $pTags);
		$stmt->fetch();
		$stmt->close();
		$results['pId'] = $pId;
		$results['pTitle'] = $pTitle;
		$results['pContent'] = $pPost;
		$results['pTime'] = $ptime;
		$results['pAuthor'] = $this->getAuthorNameFromID((int)$pAuthorId);
		$results['pTags'] = $this->splitTags($pTags);
		$results['numComments'] = $this->getNumComments($pId);
		if ($results['numComments'] > 0) {
			$results['comments'] = $this->getComments($pId);
		}
		return $results;
	}
	
/**
*
* Gets the name of the author rather than the ID
*
*@param $aID The Id of the author to look up
*@return The author's name
*/
	private function getAuthorNameFromID($aID)	{
		$stmt = $this->dbcon->prepare('SELECT author_name FROM authors WHERE author_id = ?');
		$stmt->bind_param('i', $aID);
		$stmt->execute();
		$stmt->bind_result($aName);
		$stmt->fetch();
		return $aName;
	}
	
/**
 * 
 * Separates the tags
 * 
 * Splits the tags apart so that an array of links to searches for each tag can be built
 * 
 * @param $tags Tags from the database
 * @return An array of strings containing HTML links to a search for each tag
 */
	private function splitTags($tags)	{
		$tagsList[] = array();
		$j = 0;
		for ($i = 0; $i < strlen($tags); $i++)	{
			$char = $tags[$i];
			if (strcmp($char, '#') != 0)	{
				$tempString .= $char;
			}
			else	{
				$tagsList[$j] = ("<a href=\"search.php?tag='" . $tempString . "'\">" . $tempString . "</a>\n");
				$tempString = '';
				$j++;
			}
		}
		return $tagsList;
	}

/**
 * 
 * Gets the number of comments that a post has
 * 
 * @param $pId The post ID to get the comments for
 * @return The number of comments
 */
	private function getNumComments($pId)	{
		$stmt = $this->dbcon->prepare("SELECT COUNT(comment_id) FROM comments WHERE post_id = ?");
		$stmt->bind_param('i', $pId);
		$stmt->execute();
		$stmt->bind_result($count);
		$stmt->fetch();
		return $count;
	}
	
/**
* 
* Gets the comments for a post
* 
* @param $pId The post ID to get the comments for
* @return An array of comment data.
 */
	private function getComments($pId)	{
		$stmt = $this->dbcon->prepare("SELECT comment_title, comment_content, comment_author FROM comments WHERE post_id = ?");
		$stmt->bind_param('i', $pId);
		$stmt->execute();
		$stmt->bind_result($cTitle, $comment, $cAuthor);
		$results[] = array();
		$i = 0;
		while ($stmt->fetch())	{
			$results[$i]['cTitle'] = $cTitle;
			$results[$i]['comment'] = $comment;
			$results[$i]['cAuthor'] = $cAuthor;
			$i++;
		}
		foreach ($results as &$j)	{
			$aID = $j['cAuthor'];
			$j['cAuthor'] = $this->getAuthorNameFromID((int)$aID);
		}
		return $results;
	}

}	//End of class viewPostss
?>